package j2024.j202407;

import java.util.*;

public class j0716 {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        float a = in.nextFloat();
        char b = in.next().charAt(0);
        int price = 0;
        if(b=='y'){
            price+=5;
        }
        if(a<=1){
            price+=20;
        }else {
            price+=20;
            a-=1;
            int aa=0;
            if(a%1!=0){
                aa = (int)a+1;
            }else {
                aa = (int)a;
            }
            price+=aa;
        }
        System.out.println(price);
    }
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int[] cost = new int[a];
        for (int i = 0; i < a; i++) {
            cost[i] = in.nextInt();
        }
        int[] ret = new int[a+1];
        ret[0] = ret[1] = 0;
        for (int i = 2; i <= a; i++) {
            ret[i] = Math.min(ret[i-1]+cost[i-1],ret[i-2]+cost[i-2]);
        }
        System.out.println(ret[a]);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        String s1 = in.next();
        String s2 = in.next();
        if(s1==null || s2==null){
            System.out.println(-1);
        }
        else {
            String[] str = new String[a];
            int a1 = -1;
            int a2 = -1;
            int ret = Integer.MAX_VALUE;
            for (int i = 0; i < a; i++) {
                str[i] = in.next();
                if(str[i].equals(s1)){
                    a1 = i;
                    if(a2!=-1) {
                        ret = Math.min(ret,Math.abs(a1-a2));
                    }
                }else if(str[i].equals(s2)){
                    a2 = i;
                    if(a1!=-1){
                        ret = Math.min(ret,Math.abs(a1-a2));
                    }
                }
            }
            System.out.println(ret>a?-1:ret);
        }


    }
    //542.01
    int[] dx = {0,0,-1,1};
    int[] dy = {1,-1,0,0};
    public int[][] updateMatrix(int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        int[][] dis = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dis[i][j] = -1;
            }
        }
        Queue<int[]> q = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(mat[i][j]==0){
                    q.add(new int[]{i,j});
                    dis[i][j] = 0;
                }
            }
        }
        while(!q.isEmpty()){
            int[] tmp = q.poll();
            int a = tmp[0],b = tmp[1];
            for (int i = 0; i < 4; i++) {
                int x = a+dx[i];
                int y = b+dy[i];
                if(x>=0 && x<m && y>=0 && y<n && dis[x][y]==-1){
                    dis[x][y] = dis[a][b]+1;
                    q.add(new int[]{x,y});
                }
            }
        }
        return dis;
    }
}
